package cn.yanhu.kuwanapp.common.activity

import android.annotation.SuppressLint
import android.net.http.SslError
import android.os.Build
import android.webkit.*
import cn.woochen.comlib.BaseActivity
import cn.woochen.comlib.BaseViewModel
import cn.woochen.comlib.util.UserPref
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.databinding.ActivityBaseWebviewBinding
import cn.yanhu.kuwanapp.utils.constans.Constants
import cn.yanhu.kuwanapp.utils.ext.logcom
import cn.yanhu.kuwanapp.utils.helper.WebViewHelper
import cn.yanhu.kuwanapp.widgets.LollipopFixedBridgeWebView
import java.lang.reflect.InvocationTargetException


/**
 * 带标题的webView基类
 * @param T:BaseViewModel
 */
abstract class BaseTitleWebviewActivity<T : BaseViewModel> : BaseActivity<ActivityBaseWebviewBinding, T>() {

    override fun setContentView(): Int = R.layout.activity_base_webview

    override fun initData() {
        setStatusBarStyle(false)
        showLoading(true)
        WebViewHelper.configSetting(mContext,mBinding.webView, {
            showContent()
        },{
            mBinding.tvTitle.text = it
        })
        initContentData()
    }

    abstract fun initContentData()

    protected fun getWebView() = mBinding.webView

    protected fun getTitleView() = mBinding.titleView

    override fun initListener() {
        mBinding.ivBack.setOnClickListener {
            finish()
        }
    }

    private var hasLoadRes = false

    /**
     * 设置标题
     */
    private fun receiveTitle(title: String) {
        mBinding.tvTitle.text = title
    }

    override fun back() {
        if (mBinding.webView.canGoBack()) {
            mBinding.webView.goBack()
        } else {
            finish()
        }
    }

    override fun onPause() {
        super.onPause()
        mBinding.webView.onPause()
    }

    override fun onResume() {
        super.onResume()
        mBinding.webView.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding.webView.apply {
            onResume()
            removeAllViews()
            stopLoading()
            destroy()
        }
    }
}